package com.sinothk.client.user.controller;

import com.sinothk.client.user.entity.LoginEntity;
import com.sinothk.client.user.entity.UserEntity;
import com.sinothk.server.support.author.TokenUtil;
import com.sinothk.server.support.entity.ResultData;
import com.sinothk.server.support.utils.StringUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(tags = "认证鉴权")
@RestController
@RequestMapping("/auth")
public class AuthController {

    @ApiOperation(value = "登录_账号密码", notes = "账号密码登录")
    @PostMapping("/login")
    public ResultData<LoginEntity> login(@ApiParam(value = "账号", required = true) @RequestParam("account") String account,
                                         @ApiParam(value = "密码", required = true) @RequestParam("password") String password) {
        LoginEntity loginEntity = new LoginEntity();

        UserEntity user = new UserEntity();
        user.setUserName("梁玉涛");
        loginEntity.setUserEntity(user);

        Map<String, Object> claimsInfo = new HashMap<>();
        claimsInfo.put("uid", "123456");
        claimsInfo.put("account", account);
        claimsInfo.put("nick_name", "X-rapido");
        loginEntity.setToken(TokenUtil.createToken(claimsInfo, TokenUtil.TIME_DAY_15));

        return new ResultData<LoginEntity>().getSuccess(loginEntity);
    }

    @ApiOperation(value = "注册_用户信息", notes = "注册用户信息")
    @PostMapping("/register")
    public ResultData<LoginEntity> register(@ApiParam(value = "账号", required = true) @RequestBody UserEntity userEntity) {
        LoginEntity loginEntity = new LoginEntity();
        userEntity.setUserAccount("12345");
        loginEntity.setUserEntity(userEntity);

        Map<String, Object> claimsInfo = new HashMap<>();
        claimsInfo.put("uid", "123456");
        claimsInfo.put("account", userEntity.getUserAccount());
        claimsInfo.put("nick_name", "X-rapido");
        loginEntity.setToken(TokenUtil.createToken(claimsInfo, TokenUtil.TIME_DAY_15));

        return new ResultData<LoginEntity>().getSuccess(loginEntity);
    }

    @ApiOperation(value = "Token刷新", notes = "传入旧Token，获取新Token")
    @GetMapping("/refreshToken")
    public ResultData<LoginEntity> refreshToken(@ApiParam(value = "验证Token", type = "header", required = true) @RequestHeader(value = "token") String token) {

        Claims tokenInfo = TokenUtil.openToken(token);
        String account = tokenInfo.get("account").toString();
        if (StringUtil.isEmpty(account)) {
            return new ResultData<LoginEntity>().getError("Token解析失败");
        }

        LoginEntity loginEntity2 = new LoginEntity();
        UserEntity user2 = new UserEntity();
        user2.setUserName("梁玉涛1");
        user2.setUserPassword("123456");
        loginEntity2.setUserEntity(user2);

        Map<String, Object> claimsInfo = new HashMap<>();
        claimsInfo.put("uid", "123456");
        claimsInfo.put("account", account);
        claimsInfo.put("nick_name", "X-rapido");

        loginEntity2.setToken(TokenUtil.createToken(claimsInfo, TokenUtil.TIME_DAY_15));

        return new ResultData<LoginEntity>().getSuccess(loginEntity2);
    }

}